<?php
/**
 * @Author:      ZH
 * @DateTime:  2017-07-13 13:38:30
 * @Description: 金蝶数据
 */
class JinDie extends Eloquent
{
//    protected $table = "activity_city";

    /**
     * 多账套查询接口
     * UTF-8
     * GET
     * /api/exportAccountBook
     */
    //request
    public static function getDataQuery(){
        $url = Config::get('config.k3_account_book_url');
        $now = Common::currentTimeString();
        $data = ['timestamp'=>$now];
        return JinDie::prepareRequest($url,$data,false);
    }
    /**
     * 多账套科目查询接口
     * UTF-8
     * GET
     * /api/exportAccount
     */
    //request
    public static function getAccountDataQuery(){
        $url = Config::get('config.k3_account_url');
        $now = Common::currentTimeString();
        $code = 'AIS20130723113852';
        $data = ['timestamp'=>$now,'FAccountCode'=> $code];
        return JinDie::prepareRequest($url,$data,false);
    }
    /**
     * 多账套核算维度查询
     * UTF-8
     * GET
     * /api/exportItemInfo
     */
    //request
    public static function getItemDataQuery(){
        $url = Config::get('config.k3_account_item_url');
        $now = Common::currentTimeString();
        $code = 'AIS20130723113852';
        $class_code = '';
        $data = ['timestamp'=>$now,'FAccountCode'=> $code,'FItemClassCode'=>$class_code ];
        return JinDie::prepareRequest($url,$data,false);
    }
    /**
     * 凭证导入
     * UTF-8
     * POST
     * /api/importVoucher
     */
    //request
    public static function postVoucher(){
        $url = Config::get('config.k3_account_import_url');
        $now = Common::currentTimeString();
        $code = 'AIS20130723113852';
//        $params = '{"mainVoucher":[{"FReference":"导入凭证测试","FTranType":"1","FTransDate":"2017-4-27 15:04:00","FDate":"2017-4-27 15:04:00","FCreditTotal":"1111","FDebitTotal":"1111","FEntryCount":"2","FPosterID":"452","FPreparerID":"452","FAttachments":"0","FGroupID":"付","entryVoucher":[{"FEntryID":0,"FExplanation":"借给测试账号2","FAccountID":"1001","FAmount":"1111","FAmountFor":"1111","FExchangeRate":"1","FCurrencyID":"1","FDC":"1","f_1":"002"},{"FEntryID":1,"FExplanation":"从银行取出2","FAccountID":"1001","FAmount":"1111","FAmountFor":"1111","FExchangeRate":"1","FCurrencyID":"1","FDC":"0","f_3":"008"}]}]}';
        //原始数据
        try {
            $original_data = JinDie::prepareData();
        }catch(Exception $e){
            Log::debug('统计原始数据出错 ：' . $e->getMessage());
        }
        if(isset($original_data)) {
            //组织 mainVoucher
            try {
                $params = ['mainVoucher' => JinDie::organizeParama($original_data)];
            } catch (Exception $e) {
                Log::debug('组织 mainVoucher　出错 ：' . $e->getMessage());
            }
            if(isset($params)) {
                Log::debug($params);
                //json
                $params_json = json_encode($params);
                $data = ['timestamp' => $now, 'FAccountCode' => $code, 'params' => $params_json];
                return JinDie::prepareRequest($url, $data, true);
            }
        }
        return null;
    }
    /**
     * 辅助
     */
    //组装请求
    public static function prepareRequest($url,$data,$ispost){
        ksort($data);
        //拼sign
        $sign = JinDie::getSign($data);
        //拼 url
        $data['sign'] = $sign;
        //发起请求
        $result = Common::repeatCurl($url,$data,$ispost);
        return $result['result'];
    }
    //sign
    public static function getSign($data){
        $apiSKey = Config::get('config.k3_apiSKey');
        $apiEKey = Config::get('config.k3_apiEKey');
        $sign_array = [
            'apiSKey'=> $apiSKey,
            'apiEKey'=> $apiEKey,
            'data'=> $data,
        ];
        $sign = Common::getSignString($sign_array);
        return $sign;
    }
    //准备数据
    public static function prepareData(){
        $begin_date = '2017-09-01 00:00:00';
        $end_date = "2017-09-30 23:59:59";
        $shop_id = 1;
        $data_id = 0;
        $original_data = Report::get_supply_stock_new($begin_date,$end_date,$data_id,$shop_id);
        $sale_total = 0;
        $pay_array = Config::get('total.cost_sort');
        foreach ($original_data as $_r) {
            if(isset($pay_array[$_r['level_1_id']])){
                $pay_array[$_r['level_1_id']]['start_amount']=$pay_array[$_r['level_1_id']]['start_amount']+$_r['start_amount'];
                $pay_array[$_r['level_1_id']]['storage_amount']=$pay_array[$_r['level_1_id']]['storage_amount']+$_r['into_amount']+$_r['pandian_amount']-$_r['supplier_amount'];
                $pay_array[$_r['level_1_id']]['sale_amount']=$pay_array[$_r['level_1_id']]['sale_amount']+$_r['saleout_amount']+$_r['orderout_amount']-$_r['return_into_amount']+$_r['diaochu_amount_xs'];
                $pay_array[$_r['level_1_id']]['gx_amount']=$pay_array[$_r['level_1_id']]['gx_amount']+$_r['diaochu_amount_ly'];
                $pay_array[$_r['level_1_id']]['dc_amount']=$pay_array[$_r['level_1_id']]['dc_amount']+$_r['diaochu_amount_dc'];

                $pay_array[$_r['level_1_id']]['end_amount']=$pay_array[$_r['level_1_id']]['end_amount']+$_r['end_amount'];
            }else{
                $pay_array[$_r['level_1_id']]=array(
                    'level_id'=>$_r['level_1_id'],
                    'level_name'=>$_r['level_1_name'],
                    'start_amount' => $_r['start_amount'],
                    'storage_amount' => $_r['into_amount']+$_r['pandian_amount']-$_r['supplier_amount'],
                    'sale_amount' => $_r['saleout_amount']+$_r['orderout_amount']-$_r['return_into_amount']+$_r['diaochu_amount_xs'],
                    'gx_amount' => $_r['diaochu_amount_ly'],
                    'dc_amount' => $_r['diaochu_amount_dc'],
                    'end_amount' => $_r['end_amount'],
                );
            }
            $sale_total += $_r['saleout_amount']+$_r['orderout_amount']-$_r['return_into_amount']+$_r['diaochu_amount_xs'];
        }
        $pay_array['total']['sale_total'] = $sale_total;
        return $pay_array;
    }
    //params
    public static function organizeParama($original_data){
        $original_data1 = $original_data;
        unset($original_data1['total']);
        $mainVoucher = [];
        $mainVoucher['FReference'] = '导入凭证测试';
        $mainVoucher['FTranType'] = '1';
        $mainVoucher['FGroupID'] = '记';
        $mainVoucher['FTransDate'] = date('Y-m-d 00:00:00',time());
        $mainVoucher['FDate'] = date('Y-m-d 00:00:00',time());
        $mainVoucher['FCreditTotal'] = round($original_data['total']['sale_total'] / 100,2);
        $mainVoucher['FDebitTotal'] = round($original_data['total']['sale_total'] / 100,2);
        $mainVoucher['FEntryCount'] = count($original_data1);
        $mainVoucher['FPosterID'] = '002';
        $mainVoucher['FPreparerID'] = '004';
        $mainVoucher['FAttachments'] = 0;
        $entryVoucher = [];
        $i = 0;
        $child = [];
        $child['FEntryID'] = $i;
        $child['FExplanation'] = '测试' . $i;
        $child['FAccountID'] = '5401';
        $child['FAmount'] = round($original_data['total']['sale_total'] / 100,2);
        $child['FAmountFor'] = round($original_data['total']['sale_total'] / 100,2);
        $child['FExchangeRate'] = 1;
        $child['FCurrencyID'] = 1;
        $child['FDC'] = '1';
        $child['f_002'] = '02.02';
        $entryVoucher[] = $child;
        $i += 1;
        foreach ($original_data1 as $val){
            $child = [];
            $child['FEntryID'] = $i;
            $child['FExplanation'] = '测试' . $i;
            $child['FAccountID'] = '1151';
            $child['FAmount'] = round($val['sale_amount'] / 100,2);
            $child['FAmountFor'] = round($val['sale_amount'] / 100,2);
            $child['FExchangeRate'] = 1;
            $child['FCurrencyID'] = '1';
            $child['FDC'] = '0';
            $child['f_008'] = '51.15';
            $entryVoucher[] = $child;
            $i += 1;
        }
        $mainVoucher['entryVoucher'] = $entryVoucher;
        return [$mainVoucher];
    }

}